package com.zzptc.lzh.myapp27;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.lzh.myapp27.Service.XinWenService;
import com.zzptc.lzh.myapp27.Service.YouBianService;
import com.zzptc.lzh.myapp27.entity.XinWen;
import com.zzptc.lzh.myapp27.entity.YouBian;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getXinWenInfo();
        //getYouBianInfo();
    }

    private void getYouBianInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://v.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YouBianService youBianService = retrofit.create(YouBianService.class);
        Call<YouBian> all = youBianService.getAll("215001", "0ee119ba8bfb3ead69fcd6ea3740aa31");
        all.enqueue(new Callback<YouBian>() {
            @Override
            public void onResponse(Call<YouBian> call, Response<YouBian> response) {
                YouBian body = response.body();
                Log.d(TAG, "onResponse: "+body.getResult().getList().get(1).getProvince());
            }

            @Override
            public void onFailure(Call<YouBian> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });
    }

    private void getXinWenInfo() {
        //创建retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://v.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建接口对象
        XinWenService xinWenService = retrofit.create(XinWenService.class);
        //调用接口对象中定义的方法
        Map<String,String>map=new HashMap<>();
        map.put("type","top");
        map.put("key","c9d0aa794164156acbd6debf20e39d68");
        Call<XinWen> all = xinWenService.getAll(map);
        //调用Call接口的异步方法
        all.enqueue(new Callback<XinWen>() {
            @Override
            public void onResponse(Call<XinWen> call, Response<XinWen> response) {
                XinWen body = response.body();
                Log.d(TAG, "onResponse: "+body.getResult().getData().get(8).getTitle());

            }

            @Override
            public void onFailure(Call<XinWen> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });

    }
}
